package com.example.demo.aspect;

import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

@Component
@Aspect
@Slf4j
public class TimeRecordAspect {
    @Around("execution(* com.example.demo.controller.*.*(..))")
    public Object timeRecord(ProceedingJoinPoint proceedingJoinPoint)  {
        //记录开始时间
        long start = System.currentTimeMillis();
        //执行目标方法
        log.info("Around 前....");
        Object result = null;
        try {
            result = proceedingJoinPoint.proceed();
        } catch (Throwable e) {
            throw new RuntimeException(e);
        }
        log.info("Around 后....");
        //记录结束时间
        long end = System.currentTimeMillis();
        //记录耗时

        log.info(proceedingJoinPoint.getSignature()+"耗时: "+(end-start)+"ms");
        return result;
    }


    @Before("execution(* com.example.demo.controller.*.*(..))")
    public void befor(){
        log.info("befro.......");
    }
    @After("execution(* com.example.demo.controller.*.*(..))")
    public void after(){
        log.info("after.......");
    }
    @AfterReturning("execution(* com.example.demo.controller.*.*(..))")
    public void AfterReturning(){
        log.info("AfterReturning.......");
    }
    @AfterThrowing("execution(* com.example.demo.controller.*.*(..))")
    public void AfterThrowing(){
        log.info("AfterThrowing.......");
    }




}
